package com.garrett.demo.sys.service;

import cn.smalltool.core.utils.IdcardUtil;
import com.garrett.demo.sys.dao.UserDao;
import com.garrett.demo.sys.dao.UserDataBaseDemo;
import com.garrett.demo.sys.entity.Account;
import com.garrett.demo.sys.entity.SimsStudent;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AccountServiceImp extends AbstractAccountService {
    UserDao userDao;

    {
        userDao = new UserDataBaseDemo();
    }

    /**
     * 验证信息
     * @param account 账户名
     * @param password 密码
     * @return 用户信息
     */
    public SimsStudent checkLogin(String account, String password){

        Account acc = userDao.getUserByAccount(account);
        SimsStudent stu = userDao.getStudentById(account);
        if(acc.getPassword().equals(password)){
            return stu;
        }
        return null;
    }
    /**
     * 密码比对
     * @param pw1 密码1
     * @param pw2 密码2
     * @return true-密码一致 false-密码不匹配
     */
    public boolean verifyPassword(String pw1, String pw2) {
        if(pw1.equals(pw2) && pw1.length() >= 6 && pw1.length() <= 15) return true;
        return false;
    }

    /**
     * 校验身份证号码
     * @param idCard 身份证号码
     * @return true-验证成功 false-失败
     */
    public boolean verifyIdCard(String idCard)
    {
        SimsStudent[] ss = userDao.getAllStudent();
        IdcardUtil idcardUtil = new IdcardUtil();
        if(idCard != null ) {
            return idcardUtil.isValidCard(idCard);
        }
        return false;
    }
    public boolean verifyGender(String gender,String idCard)
    {
        IdcardUtil idcardUtil = new IdcardUtil();
        if(gender == null) return false;

        return gender.equals(idcardUtil.getGenderByIdCard(idCard) == 1 ? "男" : "女") ;
    }
    /**
     * 校验学号
     * @param id 学号
     * @return true-验证成功 false-失败
     */
    public boolean verifyStuId(String id) {
        for(int i = 0 ; i < id.length(); i ++) {
            if(id.charAt(i) < '0' || id.charAt(i) > '9')
                return false;
        }
        if(id.length() != 9) return false;
        return true;
    }

    public  boolean verifyPhone(String mobile) {

        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m;
        if(mobile.charAt(0) == '+' && mobile.charAt(1) == '8' && mobile.charAt(2) == '6')
        {
            String s = mobile.substring(3);
            m = p.matcher(s);
        } else {
            m = p.matcher(mobile);
        }
        return m.matches();
    }

    public boolean verifyEmail(String email) {
        if ((email != null) && (!email.isEmpty())) {
            return Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", email);
        }
        return false;
    }

    public void createAccount(Object... obj)
    {

    }

    public void deleteAccount(String account)
    {
        userDao.delAccountById(account);
        userDao.delStudentInfo(account);
    }

    public Account[] getAllAccounts()
    {
        int Account_num =  userDao.getAccount_num();
        if(Account_num == 0){
            return null;
        }
        return Arrays.copyOfRange(userDao.getAccounts(),0, Account_num);
    }

    public SimsStudent[] getAllStudents()
    {
        int Student_num =  userDao.getStudent_num();
        if(Student_num == 0){
            return null;
        }
        return Arrays.copyOfRange(userDao.getStudents(),0, Student_num);
    }

    public SimsStudent getStudentById(String stuId)
    {
        SimsStudent[] students = getAllStudents();
        int Student_num =  userDao.getStudent_num();
        for (int i = 0 ; i < Student_num ; i ++){
            if(students[i].getStudentId().equals(stuId)){
                return students[i];
            }
        }
        return null;
    }

    public void modify(Object... obj)
    {

    }


}
